home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Booting Gallery / Booting Gallery (source) / (Libraries) / Hubauer / DArray.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  1.2 KB  |  102 lines  |  [TEXT/BROW]

  1. #ifndef _DArray_h_
  2. #define _DArray_h_
  3.  
  4. template <class T>
  5. class DArray
  6. {
  7. public:    
  8.     DArray()
  9.     {
  10.         _a = NULL;
  11.         _size = 0;
  12.     }
  13.     
  14.     DArray(UInt32    size)
  15.     {
  16.         _a = NULL;
  17.         _size = 0;
  18.         Reallocate(size);
  19.     }
  20.     
  21.     DArray(const DArray& inCopyThis)
  22.     {
  23.         _a = NEW T[inCopyThis._size];
  24.         if(_a != NULL){
  25.             _size = inCopyThis._size;
  26.             for(UInt32 z = 0;z<_size;z++){
  27.                 _a[z] = inCopyThis._a[z];
  28.             }
  29.         }
  30.     }
  31.     
  32.     ~DArray()
  33.     {
  34.         delete [] _a;
  35.     }
  36.     
  37.     OSErr    Reallocate(UInt32 size)
  38.     {
  39.         delete [] _a;
  40.         _a = NEW T[size];
  41.         if(_a == NULL){
  42.             _size = 0;
  43.             return -108;
  44.         }else{
  45.             _size = size;
  46.             return noErr;
  47.         }
  48.     }
  49.     
  50.     void    SetSize(UInt32 newSize)        // only use for shortening the list
  51.     {
  52.         _size = newSize;
  53.     }
  54.     
  55.     DArray&    operator =(const DArray& inCopyThis)
  56.     {
  57.         delete [] _a;
  58.         _size = 0;
  59.         _a = NEW T[inCopyThis._size];
  60.         if(_a != NULL){
  61.             _size = inCopyThis._size;
  62.             for(UInt32 z = 0;z<_size;z++){
  63.                 _a[z] = inCopyThis._a[z];
  64.             }
  65.         }
  66.         
  67.         return *this;
  68.     }
  69.     
  70.     T&    operator[](int index)
  71.     {
  72.         return *(_a + index);
  73.     }
  74.     
  75.     const T&    operator[](int index) const
  76.     {
  77.         return *(_a + index);
  78.     }
  79.     
  80.     UInt32    Size() const
  81.     {
  82.         return _size;
  83.     }
  84.     
  85.     operator T*()
  86.     {
  87.         return _a;
  88.     }
  89.     
  90.     
  91. private:
  92.     
  93.     T*    _a;
  94.     UInt32    _size;
  95. };
  96.  
  97.  
  98.  
  99.  
  100. #endif
  101.  
  102.